/*
Author: winter
Date: Jan. 06, 2004
Purpose: check holiday
*/

#include "uutil.h"

#define tsec 345600 //four days sec
#define SIZE 1024
#define	LOGPATH	"/export/home/stock/log"

MYSQL *conn, mysql;
char gszdate[20], gdldate[20];

int fnMySQLInit();
int chkholiday();
void delshmDb();
void mysql_Connect( char *sql );

void thistimer()
{
	time_t tp,t;
        struct tm tmstr;
        char szLog[20];
	
	t = time( &tp );
        tmstr = *localtime( &t );
        sprintf( gszdate, "%04d%02d%02d", tmstr.tm_year+1900, tmstr.tm_mon+1, tmstr.tm_mday );        
        sprintf( szLog, "%s/DEL.log", LOGPATH );
        log=log_open( szLog, 0); 
        
        t -= (int) tsec; 
        tmstr = *localtime( &t ); 
        sprintf( gdldate, "%04d%02d%02d", tmstr.tm_year+1900, tmstr.tm_mon+1, tmstr.tm_mday );      
}

int 
fnMySQLInit()
{
	mysql_init( &mysql );
	if ( ( conn = mysql_real_connect( &mysql, "192.168.0.233",  "stock",  "stov2", "stock_db", 0, NULL, 0 ) ) == NULL )
	{
		lprintf( log, ERROR ,"%s\n", mysql_error(&mysql));
		return 0;
	}
	else return 1;
}

int 
chkholiday()
{
	unsigned char szSQL[1000];
	MYSQL_RES *result;
	int num_rows=0;
	
	if( fnMySQLInit() )
	{
		sprintf( szSQL, " select day from holiday where day='%s'", gszdate );
		if ( ! mysql_real_query( &mysql, szSQL, strlen(szSQL) ) )
  		{
  			result=mysql_store_result( &mysql );
  			num_rows=mysql_num_rows( result );
			mysql_free_result( result );
			return num_rows;
		}
	}
	return num_rows;
}

void mysql_Connect( char *sql )
{
	MYSQL_RES *result;
  	if ( ! mysql_real_query( &mysql, sql, strlen( sql ) ) )
  	{
  		result=mysql_store_result( &mysql );
  		mysql_free_result( result );
	}
}

void
delshmDb()
{
	char szLog[100], szSQL[1000], *DBNAME[3]={"TSE","OTC","emg"};
	char *TBNAME[11]={"f2","f3","f4","f5","f6","f7","f8","f9","f10","f11","f12"};
	int x, i;
	for( x=0; x<3 ; x++)
	{
		for( i=0; i<12; i++)
		{
			
			if( x && i==11 ) break;
			sprintf( szSQL,"delete from %s.%s where rec_date < '%s'", DBNAME[x], TBNAME[i], gdldate);
			mysql_Connect( szSQL );
        		lprintf(log, INFO, " delete %s.%s ok!!\n" , DBNAME[x], TBNAME[i]);
        		
        	}
        	sprintf( szLog, "/bin/rm -rf %s/%sCPOOL.log", LOGPATH, DBNAME[x]);
        	if(! system( szLog ) ) lprintf(log, INFO, " Delete %s CPOOL LOG ok!!\n", DBNAME[x] );
        	sprintf( szLog, "/bin/rm -rf %s/%sERR.log", LOGPATH, DBNAME[x] );
        	if(! system(szLog) ) lprintf(log, INFO, " Delete %s ERR LOG ok!!\n", DBNAME[x] );
        	if( x==2 ) 
        	{
        		sprintf( szSQL,"delete from %s.f4a where rec_date < '%s'", DBNAME[x], gdldate);
        		lprintf(log, INFO, " delete emg.f4a ok!!\n");
        		sprintf( szSQL, "delete from emg.tick where rec_date < '%s'", gszdate);
        		mysql_Connect( szSQL );
			lprintf(log, INFO, " delete emg.tick ok!!\n");
			sprintf( szSQL, "delete from emg.last_tick where rec_date < '%s'", gszdate);
  			mysql_Connect( szSQL );
  			lprintf(log, INFO, " delete emg.last_tick ok!!\n");
        		mysql_Connect("delete from emg.emg_stock");
        		lprintf(log, INFO, " delete emg.emg_stock ok!!\n");
        	}
        }
        mysql_Connect("delete from stock_db.last_tick");
        lprintf(log, INFO, " delete stock_db.last_tick ok!!\n");
        mysql_Connect("delete from stock_db.tick");
        lprintf(log, INFO, " delete stock_db.tick ok!!\n");
        if(! system("/bin/ipcrm -M 0x835")) lprintf(log, INFO, " delete otc shm ok!!\n");
        if(! system("/bin/ipcrm -M 0x44d")) lprintf(log, INFO, " delete tse shm ok!!\n");
        mysql_close( conn );
         
}

int main()
{
	int c;
	thistimer();
	if ( fnMySQLInit())
	if ( chkholiday()) exit(0);
	delshmDb();
	if (! system("/export/home/stock/tse/tse > /dev/null &")) lprintf(log, INFO, " start tse ok!!\n");
	if (! system("/export/home/stock/otc/otc > /dev/null &")) lprintf(log, INFO, " start otc ok!!\n");
	if (! system("/export/home/stock/emg/emg /dev/tty00 > /dev/null &")) lprintf(log, INFO, " start emg ok!!\n");
	log_close( log );
	return 0;
}